<template>
  <div class="search-container" v-loading="loading">
    <el-form v-model="searchPage" inline="true" >
      <el-form-item label="登记号：">
        <el-input v-model="searchPage.regNum" />
      </el-form-item>
      <el-form-item label="姓名：">
        <el-input v-model="searchPage.name" />
      </el-form-item>
      <el-form-item label="样本编码：">
        <el-input v-model="searchPage.code" />
      </el-form-item>
      <el-form-item label="创建时间：">
        <el-date-picker
          v-model="searchPage.createTimes"
          type="daterange"
          range-separator="To"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :size="size"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="data-container">
      <el-table :data="tableData" style="width: 100%">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" width="80" label="序号"/>
      <el-table-column prop="diagnose.patient.regNum" label="登记号" width="160" />
      <el-table-column prop="diagnose.patient.name" label="姓名" width="120" />
      <el-table-column prop="typeId" label="样本类型" width="120">
        <template #default="scope">
          <el-select v-model="scope.row.typeId" placeholder="" name="typeId" disabled >
            <el-option value="1" label="血清"/>
            <el-option value="2" label="唾液"/>
            <el-option value="3" label="血浆"/>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="id" label="样本编号" width="120" />
      <el-table-column prop="surplus" label="剩余量" width="100"/>
      <el-table-column prop="serNo" label="流水号" width="200" />
      <el-table-column prop="position" label="位置" width="200" />
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column fixed="right" label="操作" width="140">
        <template #default="scope">
          <el-button text type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button text type="danger" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
     <el-pagination
      v-model:currentPage="searchPage.current"
      v-model:page-size="searchPage.pageSize"
      :page-sizes="[20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableDatas.total"
      hide-on-single-page=true
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <SampleForm :isShow="diaVisible" @diaCancel="diaCnacel" :selectRow="selectRow"></SampleForm>
  </div>
</template>

<script setup>
import { ref, onActivated } from 'vue'
import { ElMessage } from 'element-plus'
import { list, deleteById } from '@/api/common/common'
import { pageSearch } from '@/api/grk/index'
import SampleForm from '../form/index.vue'
const tableDatas = ref({})

const tableData = ref([])

const diaVisible = ref(false)

const selectRow = ref({})

const searchPage = ref({
  pageSize: 20,
  current: 1,
  sortBy: ['id']
})

const searchFunction = async () => {
  pageSearch(searchPage.value, 'grk/sample').then(res => {
    tableData.value = res.content
    tableDatas.value.total = parseInt(res.totalElements)
    ElMessage.success('成功')
  }).catch(err => {
    ElMessage.success('失败' || err.message)
  })
}

onActivated(searchFunction())

const onSubmit = () => {
  searchFunction()
}

const onReset = () => {
  searchPage.value = {
    pageSize: 20,
    current: 1,
    sortBy: ['id']
  }
  // 处理查询
  searchFunction()
}

const handleSizeChange = pageSize => {
  searchPage.value.pageSize = pageSize
  // 执行查询操作
  searchFunction()
}

const handleCurrentChange = current => {
  searchPage.value.current = current
  // 执行查询操作
  searchFunction()
}

const handleDelete = id => {
  deleteById(id, 'grk/sample').then(res => {
    ElMessage.warning('删除成功')
    // 重新刷新
    searchFunction()
  })
}

const handleEdit = row => {
  diaVisible.value = true
  selectRow.value = row
  ElMessage.success('编辑')
}

const diaCnacel = () => {
  diaVisible.value = false
}
</script>

<style>

</style>
